package edu.ncu.controller;

import edu.ncu.dao.UserDao;
import edu.ncu.entity.Contact;
import edu.ncu.entity.User;
import edu.ncu.utils.ChatMessageUtils;
import edu.ncu.utils.RedisUtil;
import edu.ncu.websocket.MyWebSocketHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.ejb.PostActivate;
import javax.jws.WebParam;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Controller
public class ChatController {
    @Autowired
    private UserDao userDao ;
    @Autowired
    private RedisUtil redisUtil ;
    @Autowired
    private ChatMessageUtils chatMessage ;
    @Autowired
    private MyWebSocketHandler webHandler ;

    @RequestMapping("/chat")
    public String chat(HttpServletRequest request,Model model){
        User user = (User) request.getSession().getAttribute("user") ;
        Set<User> chatUsers = null ;
        if (user != null) {
            // 获取用户的聊天消息列表
            // 消息列表保存在redis里面
            chatUsers = redisUtil.getChatUserList(user.getUserId()) ;
            //chatUsers = chatMessage.getChatMessageList(user.getUserId())
        }
        model.addAttribute("messageList",chatUsers) ;
        request.getSession().setAttribute("messageList",chatUsers);
        return "chat" ;
    }

    /**
     * 点击某位好友，点击发送消息，向某个好友发送消息 fid表示好友的id
     * @{/chat/{fid}/firend(fid=friend.userId)}
     * @return
     */
    @PostMapping("/chat/{fid}/friend")
    public String chat(@PathVariable int fid , HttpServletRequest request, Model model ) {
        // User user = userDao.findByUserId(1) ;
        User friend = userDao.findByUserId(fid) ; // 找到要发送消息的朋友
        model.addAttribute("friend",friend) ;
        // 消息列表保存在 redis 里面
        User user = (User) request.getSession().getAttribute("user") ;
        redisUtil.addChatUser(user.getUserId(),friend);
        Set<User> messageList = redisUtil.getChatUserList(user.getUserId()) ;
                // messageList = (List<User>) request.getSession().getAttribute("messageList") ;
        // messageList.add(friend) ;
        request.getSession().setAttribute("messageList",messageList);
        // model.addAttribute("user",user) ;
        model.addAttribute("messageList",messageList) ;
        webHandler.setFriendId(fid);

        // 消息发送
//        List<Contact> list = webHandler.getOffMessage(user.getUserId());
//        list.forEach((e) -> {
//            if (e.getSenderId() == fid) {
//                try {
//                    webHandler.sendMessageToUser(e) ;
//                } catch (IOException ex) {
//                    ex.printStackTrace();
//                }
//            }
//        });
        return "chat" ;
    }

    /**
     * 向1发送消息
     * @param request
     * @param model
     * @return
     */
    @GetMapping("chat/1/friend")
    public String chat1( HttpServletRequest request, Model model ) {
        int fid = 1 ;
        User user = userDao.findByUserId(2) ;
        User friend = userDao.findByUserId(fid) ; // 找到要发送消息的朋友
        model.addAttribute("friend",friend) ;
        // 消息列表保存在session里面
        List<User> messageList = new ArrayList<>();

        // messageList = (List<User>) request.getSession().getAttribute("messageList") ;
        messageList.add(friend) ;
        request.getSession().setAttribute("messageList",messageList);
        // model.addAttribute("user",user) ;
        model.addAttribute("messageList",messageList) ;
        request.getSession().setAttribute("user",user);
        return "chat" ;
    }

    /**
     * 向2发送消息
     * @param request
     * @param model
     * @return
     */
    @GetMapping("chat/2/friend")
    public String chat2( HttpServletRequest request, Model model ) {
        int fid = 2 ;
        User user = userDao.findByUserId(1) ;
        System.out.println("user:"+user);
        User friend = userDao.findByUserId(fid) ; // 找到要发送消息的朋友
        model.addAttribute("friend",friend) ;
        // 消息列表保存在session里面
        List<User> messageList = new ArrayList<>();
        // messageList = (List<User>) request.getSession().getAttribute("messageList") ;
        messageList.add(friend) ;
        request.getSession().setAttribute("messageList",messageList);
        // model.addAttribute("user",user) ;
        model.addAttribute("messageList",messageList) ;
        request.getSession().setAttribute("user",user);
        return "chat" ;
    }

}
